package com.woniu.defecttracking.controller.datas;


import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.exception.RoleException;
import com.woniu.defecttracking.exception.RuleException;
import com.woniu.defecttracking.param.AddRoleRuleParam;
import com.woniu.defecttracking.service.RoleRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 倪西
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/roleRule")
public class RoleRuleController {

    @Autowired
    private RoleRuleService roleRuleService;

    /**
    * @作者: 倪西
    * @描述：新增角色和rule的关联
    * @时间：2022/1/7 21:54
    * @参数：[addRoleRuleParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/addRoleRule")
    public Result addRoleRule(@RequestBody AddRoleRuleParam addRoleRuleParam) throws RuleException, RoleException {
        Boolean bool = roleRuleService.saveRoleRule(addRoleRuleParam);
        if (bool){
            return Result.getInstance(null).setMessage("新增成功");
        }else {
            return Result.getInstance(null).setMessage("新增失败").setCode(411);
        }
    }

    /**
    * @作者: 倪西
    * @描述：根据角色查询对应的rule
    * @时间：2022/1/7 21:55
    * @参数：[id]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("/findRuleIds/{id}")
    public Result findRuleIds(@PathVariable("id") Integer id) throws RoleException {
        List<Integer> ruleIds = roleRuleService.findRuleIds(id);
        return Result.getInstance(ruleIds);
    }
}

